<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    /*  console.dir(Array);
 
     var arr = [1, 2, 3];
     console.log(arr);
     // console.log(arr.constructor === Array);
 
 
     console.dir(String);
     var str = new String("hello");
     console.log(str);
     // console.log(str.constructor === String);
 
 
     console.dir(Promise);
  */
</script>
<script>

    // 封装
    /* function myForEach(list,fn) {
        console.log(list,fn);
        // function fn(item, index, array) { // 函数定死
        //     console.log(item, index, array);
        // }
        // var list = [1, 4, 7, 8, 5, 2, 3, 6, 9];  //数组定死
        for (var i in list) {
            i = Number(i); // 循环的当前下标
            fn(list[i], i, list); // 实际参数 (循环的当前元素, 下标 原数组)
        }
    }

    myForEach([1,4,7],function(item,index){
        console.log(item,index);
    });
    myForEach([2,5,8],function(a,b){
        console.log(a,b);
    }); */


    Array.prototype._forEach = function (callback) {
        // this -> 调用此方法的实例化对象
        // for (var i in this) {
        //     i = Number(i); // 循环的当前下标
        //     callback(this[i], i, this); // 实际参数 (循环的当前元素, 下标 原数组)
        // }
        for (var i = 0; i < this.length; i++) {
            callback(this[i], i, this);
        }
    }

    Array.prototype._map = function (callback) {

        var arr = [];
        for (var i = 0; i < this.length; i++) {
            var result = callback(this[i], i, this); // 接收回调函数的返回值,放到新数组
            arr.push(result);
        }
        return arr;
    }

    Array.prototype._filter = function (callback) {
        var arr = [];
        for (var i = 0; i < this.length; i++) {
            var result = callback(this[i], i, this);
            if (!!result) { // 将回调函数的返回值作为条件, 筛选出所有满足条件的元素
                arr.push(this[i]);
            }
        }
        return arr;
    }

    Array.prototype._find = function (callback) {

        for (var i = 0; i < this.length; i++) {
            var result = callback(this[i], i, this);
            if (!!result) {
                return this[i];
            }
        }
    }

    Array.prototype._find = function (callback) {

        for (var i = 0; i < this.length; i++) {
            var result = callback(this[i], i, this);
            if (!!result) {
                return i;
            }
        }
        return -1;
    }

    var list = [1, 4, 7, 2, 5, 8, 3, 6, 9];

    list._forEach(function (item, index, array) {
        console.log(item, index, array);
    })



</script>

</html>